<% layout('/layouts/default.html', {title: '考核任务指标汇总表管理', libs: ['layout', 'dataGrid','validate','fileupload','inputmask','ueditor']}){ %>
<div class="main-content">
	<div class="box box-main">
		<#form:form id="searchForm" model="${dcSupExamSumQuotaInfo}" action="${ctx}/quota/dcSupExamSumQuotaInfo/listData" method="post" class="form-inline "
		data-page-no="${parameter.pageNo}" data-page-size="${parameter.pageSize}" data-order-by="${parameter.orderBy}">
		<div class="box-header">
			<div class="box-title">
				<#form:hidden path="exameOrg"/>
				<#form:hidden path="examId" />
				<#form:hidden path="examYear" />
				<#form:hidden path="copyVersion" />
				<#form:hidden path="versionNumber" />
				<#form:hidden path="versionName" />
				<#form:hidden path="exameOrgName" />
				<!--此处为导出时选择的列的插入位置-->
				<#form:hidden path="exportColumn" />
				<#form:hidden path="quotaTypes" />
			</div>
			<div style="float: right">
				<div class="form-group">
					<div class="control-inline">
						<button type="button"  class="btn btn-primary btn-sm" id="batchUpdateQuotaContent" title="${text('给左侧被考核单位统一修改考核指标')}"><i class="fa fa-plus">&nbsp;${text('批量修改单项指标')}</i></button>
					</div>
				</div>
				<div class="form-group">
					<div class="control-inline">
						<button type="button"  class="btn btn-primary btn-sm" id="batchAdd" title="${text('给左侧被考核单位统一新增考核指标')}"><i class="fa fa-plus">&nbsp;${text('批量新增单项指标')}</i></button>
					</div>
				</div>
				<div class="form-group">
					<button type="button" onclick="batchUpdate()" id="batch" class="btn btn-primary btn-sm">${text('批量修改')}</button>
				</div>
			</div>

		</div>
	</#form:form>
	<div class="box-body">
		<div class="ui-layout-content" id="dynamicTable">
			<table id="dataGrid"></table>
		</div>
	</div>
</div>
<% } %>

<style>

</style>
<script>
	let addRowInitData={}
	var flag = false;

	$("#quotaType").blur(function (){})
	let isPush =${toJson(isPush)};

	var options ={
		searchForm: $("#searchForm"),
		columnModel: [
			{header:'${text("主键")}', name:'id', index:'a.id' ,hidden:true,hidedlg:true},
			{header:'${text("考核任务id")}', name:'examId',hidden:true,hidedlg:true},
			{header:'${text("考核年度")}', name:'examYear', index:'a.exam_year', width:65, align:"center",hidden:true,hidedlg:true,
				editable:true, edittype:'text', editoptions:{'maxlength':'200', 'class':'form-control'},
				unformat: function(val, obj, cell){
					return $('#'+obj.rowId+'_examYear').val();
				}},
			{header:'${text("指标类型")}', name:'quotaGroup', index:'a.quota_group', width:80, align:"center",
				editable:true, edittype:'text', editoptions:{'maxlength':'200', 'class':'form-control'},
				formatter: function(val, obj, row, act){
					if (row.quotaClass=='1' && row.quotaGroup =='1'){
						return '关键指标';
					}else if(row.quotaGroup =='2'){
						return '单项指标';
					}else{
						return '';
					}
				},
				unformat: function(val, obj, cell){
					return $('#'+obj.rowId+'_quotaGroup').val();
				}
			},
			{header:'${text("指标类别")}', name:'quotaType', index:'a.quota_type', width:120, align:"center",
				formatter: function(val, obj, row, act){
					return js.getDictLabel(${@DictUtils.getDictListJson('dc_quotaType')},
							val, '${text("未知")}', true);
				},
				unformat: function(val, obj, cell){
					return $('#'+obj.rowId+'_quotaType').val();
				},
				editable:true, edittype:'select', editoptions:{'class':'form-control',
					items: $.merge([{dictLabel:'&nbsp;',dictValue:''}],
							${@DictUtils.getDictListJson('dc_quotaType')}),
					itemLabel: 'dictLabel', itemValue: 'dictValue', dataInit: function(element){
						$(element).select2().on("change",function(){$(this).resetValid()});
					}
				}
			},


			{header:'${text("序号")}', name:'sortNum', index:'a.sort_num', width:50, align:"center",
				editable:true, edittype:'text', editoptions:{'maxlength':'200', 'class':'form-control'},
				unformat: function(val, obj, cell){
					return $('#'+obj.rowId+'_sortNum').val();
				}
			},
			{header:'${text("目标内容")}', name:'quotaContent', index:'a.quota_content', width:370, align:"left",
				editable:true, edittype:'text', editoptions:{'maxlength':'200', 'class':'form-control'},
				unformat: function(val, obj, cell){
					return $('#'+obj.rowId+'_quotaContent').val();
				}
			},
			{header:'${text("单位")}', name:'quotaUnit', index:'a.quota_unit', width:60, align:"center",
				editable:true, edittype:'text', editoptions:{'maxlength':'200', 'class':'form-control'},
				unformat: function(val, obj, cell){
					return $('#'+obj.rowId+'_quotaUnit').val();
				}
			},
			{header:'${text("权数")}', name:'quotaWeight', index:'a.quota_weight', width:60, align:"center",
				formatter: function(val, obj, row, act){
					if(val == null){
						return 0.00;
					}else{
						return parseFloat(val).toFixed(2);
					}
				},
				editable:true, edittype:'text', editoptions:{'maxlength':'200', 'class':'form-control'},
				unformat: function(val, obj, cell){
					return $('#'+obj.rowId+'_quotaWeight').val();
				}
			},
			{header:'${text("考核方式")}', name:'assessMethod', index:'a.assess_method', width:93, align:"center",
				editable:true, edittype:'text', editoptions:{'maxlength':'200', 'class':'form-control'},
				formatter: function(val, obj, row, act){
					return js.getDictLabel(${@DictUtils.getDictListJson('dc_exam_assessment')},
							val, '', true);
				},
				unformat: function(val, obj, cell){
					return $('#'+obj.rowId+'_assessMethod').val();
				}
			},
			{header:'${text("本年度目标")}', name:'currentYaerGoal', index:'a.current_yaer_goal', width:65, align:"center",
				editable:true, edittype:'text', editoptions:{'maxlength':'200', 'class':'form-control'},
				unformat: function(val, obj, cell){
					return $('#'+obj.rowId+'_currentYaerGoal').val();
				}
			},
			{header:'${text("上一年度目标任务")}', name:'lastYaerGoal', index:'a.last_yaer_goal', width:70, align:"center",
				editable:true, edittype:'text', editoptions:{'maxlength':'200', 'class':'form-control'},
				unformat: function(val, obj, cell){
					return $('#'+obj.rowId+'_lastYaerGoal').val();
				}
			},
			{header:'${text("上一年度完成情况")}', name:'lastYearComplete', index:'a.last_year_complete', width:65, align:"center",
				editable:true, edittype:'text', editoptions:{'maxlength':'200', 'class':'form-control'},
				unformat: function(val, obj, cell){
					return $('#'+obj.rowId+'_lastYearComplete').val();
				}
			},
			{header:'${text("备注")}', name:'remarks', index:'a.remarks', width:90, align:"center",
				formatter: function(val, obj, row, act){
					return js.getDictLabel(${@DictUtils.getDictListJson('dc_exam_remarks')},
							val, '${text("未知")}', true);
				},
				editable:true, edittype:'text', editoptions:{'maxlength':'200', 'class':'form-control'},
				unformat: function(val, obj, cell){
					return $('#'+obj.rowId+'_remarks').val();
				}
			},
			{header:'${text("考核评分单位")}', name:'gradeOrg',width:120, align:"center",
				formatter: function(val, obj, row, act){
					// if (row.quotaClass=='1' && row.quotaGroup =='1'){
					//  return ""
					// }else{
					if (row.gradeOrg !=null ){
						return js.val(row, 'gradeOrgName');
					}else{
						return "";
					}
					// }
				},
				unformat: function(val, obj, cell){
					var str = cell.childElementCount;
					if (cell.childElementCount > 0) {
						var exameOrgCode= $('#office_'+obj.rowId+'_gradeOrgCode', cell).val();
						var exameOrgName= $('#office_'+obj.rowId+'_gradeOrgName', cell).val();
						return exameOrgCode + '|' + exameOrgName;
					}
					return cell.outerText;
				},
				editable: true, edittype: "custom", editoptions: {
					custom_element: function(val, editOptions) {
						if (val.split('|')[0] == "undefined" && val.split('|')[1] == "undefined") {
							val = "|"
						}
						return js.template('treeselectTpl', {
							id:'office_'+editOptions.id,  title: '考核评分单位',type:"text",
							name: 'gradeOrgCode', value: val.split('|')[0],
							labelName: 'gradeOrgName', labelValue: val.split('|')[1],
							url: '${ctx}/sys/office/treeUnitData', cssClass: ''
						});
					}
				}
			}
		],
		autoGridHeight: function(){return 'auto'},
		showRownum: false,
		onSelectRow: function(id, isSelect, event){

		},
		//editGrid: false,
		showFooter: true, // 是否显示底部合计行
		// 加载成功后执行事件
		ajaxSuccess: function(data){
			var info = $('#dataGrid').dataGrid('getRowData');
			$(parent.document.getElementById("dataInfo")).val(JSON.stringify(info));
			refreshZpCount();
		}
	}
	// 初始化DataGrid对象
	$('#dataGrid').dataGrid(options);

	function refresh(type,orgCode){
		let url_ = window.location.href;
		var arr= url_.split("&");
		var req = arr.slice(0,arr.length -3).join('&');
		let version =$("#versionNumber").val();
		if (type != null){
			if (orgCode == 'success'){
				window.location.replace(req+'&remarks3=0&copyVersion='+type+'&versionNumber='+type + '&examYear=' + $("#examYear").val());
			}else{
				var request_info = arr.slice(0,arr.length -5).join('&');
				window.location.replace(request_info+'&exameOrg='+orgCode+'&remarks3=0&copyVersion='+type+'&versionNumber='+type + '&examYear=' + $("#examYear").val());
				$(parent.refreshStyle());
			}
		}else{
			if (version != null){
				window.location.replace(req+'&remarks3=1&copyVersion='+version+'&versionNumber='+version + '&examYear=' + $("#examYear").val());
			}else{
				window.location.replace(req+'&remarks3=1'+ '&examYear=' + $("#examYear").val());
			}
		}
	}

	function batchUpdate() {

		let version=$("#versionNumber").val();
		if (version == ' ' || version == null){
			js.showMessage("当前版本为初始版本，请新建版本或选择版本后操作!!!")
			return;
		}
		if (version == isPush){
			js.showMessage("当前版本已发布,不允许进行此操作!!!")
			return;
		}
		$("#copyVersion").val($("#versionNumber").val());
		$(parent.document.getElementById("mainVersion")).val($("#versionNumber").val())
		$(parent.document.getElementById("version_name")).val($("#versionNumber").val())
		$(parent.document.getElementById("batch_flag")).val("1")
		var examId = $("#examId").val();
		$.ajax({
			url: "${ctx}/quota/dcSupExamSumQuotaInfo/newMakeCopy",
			type: "post",
			cache: false,
			async:false,
			data: {
				"examId":examId,
				"copyVersion":$("#copyVersion").val()
			},
			success: function(res){
				js.showMessage(res.message);
				console.log(window.parent)
				refresh();
			}
		});
	}

	function accAdd(arg1,arg2){
		var r1,r2,m;
		try{r1=arg1.toString().split(".")[1].length}catch(e){r1=0}
		try{r2=arg2.toString().split(".")[1].length}catch(e){r2=0}
		m=Math.pow(10,Math.max(r1,r2))
		return (arg1*m+arg2*m)/m
	}

	function refreshZpCount(){
		let data = $('#dataGrid').dataGrid('getRowData');
		let allQuotaWeight=0;
		let subScore=0;
		let addScore=0;
		for (let i = 0; i < data.length; i++) {
			if (data[i].quotaWeight == 'undefined' ||data[i].quotaWeight==''){
				continue;
			}
			if (data[i].quotaType == "加分项"){
				addScore=accAdd(parseFloat(data[i].quotaWeight||0),addScore);
			}else if(data[i].quotaType == "倒扣分项"){
				subScore=accAdd(parseFloat(data[i].quotaWeight||0),subScore);
			}else{
				allQuotaWeight=accAdd(parseFloat(data[i].quotaWeight||0),allQuotaWeight);
			}
		}
		$('#dataGrid').dataGrid("footerData", "set", {
			"quotaType" : "<center><span>汇总：</span></center>",
			"quotaContent" : "(单项+关键)总权数：<span>"+parseFloat(allQuotaWeight).toFixed(2)+"</span>分",
			"assessMethod" : "<span>倒扣分总权数:</span>",
			"currentYaerGoal" : "<span>"+parseFloat(subScore).toFixed(2)+"</span>分",
			"remarks": "<span>加分项总权数：</span>",
			"gradeOrg": "<span>"+parseFloat(addScore).toFixed(2)+"</span>分"
		}, false);
	}

	$("#batchUpdateQuotaContent").click(function (){
		// 版本号不能为空
		let version=$("#versionNumber").val();
		let versionName= $("#versionName").val();
		if (version == ' ' || version == null){
			js.showMessage("当前版本为初始版本，请新建版本或选择版本后操作!!!")
			return;
		}
		if (version == isPush){
			js.showMessage("当前版本已发布,不允许进行此操作!!!")
			return;
		}
		js.addTabPage(null, "修改指标","${ctx}/quota/dcSupExamSumQuotaInfo/batchUpdateQuota?examId=${dcSupExamSumQuotaInfo.examId}&examYear=${dcSupExamSumQuotaInfo.examYear}&versionNumber="+version+"&versionName="+versionName, true);
	})




	$("#batchAdd").click(function (){
		// 版本号不能为空
		let version=$("#versionNumber").val();
		if (version == ' ' || version == null){
			js.showMessage("当前版本为初始版本，请新建版本或选择版本后操作!!!")
			return;
		}
		if (version == isPush){
			js.showMessage("当前版本已发布,不允许进行此操作!!!")
			return;
		}
		// 指标id  examYear 版本号
		js.addTabPage(null, "新增指标","${ctx}/quota/dcSupExamSumQuotaInfo/batchAddQuota?examId=${dcSupExamSumQuotaInfo.examId}&examYear=${dcSupExamSumQuotaInfo.examYear}&versionNumber="+version+"&remarks2="+$("#exameOrg").val() +"&remarks3="+$("#exameOrgName").val(),true);
	})



</script>
<script id="treeselectTpl" type="text/template">//<!--<div>
<#form:treeselect id="{{d.id}}" title="{{d.title}}" name="{{d.name}}" value="{{d.value}}"
    labelName="{{d.labelName}}" labelValue="{{d.labelValue}}" url="{{d.url}}"
     class="{{d.cssClass}}" isReturnValue="false" allowClear="true" canSelectRoot="true" canSelectParent="true"
        returnFullName="false" checkbox="false" chkboxType="{'Y':'','N':''}" btnClass="btn-sm"
        placeholder="选择考核评分单位"/>
</div>//--></script>
<!--<% if(hasPermi('quota:dcSupExamSumQuotaInfo:edit')){ %>-->
<!--<% } %>-->
